package com.wnhz.work.yz.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.diycoder.citypick.widget.CityPickerPopWindow;
import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.AddPersonBean;
import com.wnhz.work.yz.bean.CustomerTypeBean;
import com.wnhz.work.yz.bean.JsonBean;
import com.wnhz.work.yz.bean.ProductBean;
import com.wnhz.work.yz.bean.RecordDetailBean;
import com.wnhz.work.yz.utils.GetJsonDataUtil;
import com.wnhz.work.yz.utils.SystemUtils;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;
import com.wnhz.work.yz.view.SwitchView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddAccurateActivity extends BaseActivity implements View.OnClickListener, CityPickerPopWindow.CityPickListener, SwitchView.OnStateChangedListener {
    private final int LOOKNUM = 987;
    private final int ADDPERSON = 988;
    private TextView address_click, look, nums, add_person, customer_address_click, customer_type;
    private LinearLayout ll_rootView;
    private EditText name, desicAdd, all_money, customer_name, customer_desicAdd;
    private LinearLayout rivla1, rivla2;
    private ImageView rivla_img2, rivla_img1;
    private boolean rival_li1, rival_li2;
    private RecyclerView recycler_person;
    private Button submit_all;
    private ArrayList<ProductBean> data = new ArrayList<>();
    private List<AddPersonBean> addPersonBean = new ArrayList<>();
    private CityPickerPopWindow mPopWindow;
    private SwitchView switchButton;


    private List<CustomerTypeBean.InfoEntity.CustomerTypeEntity> customerTypeList = new ArrayList<>();
    private List<CustomerTypeBean.InfoEntity.OperatorEntity> operator = new ArrayList<>();

    private OptionsPickerView pvOptions;
    private ArrayList<String> infoType = new ArrayList<>();
    private ArrayList<String> infoId = new ArrayList<>();
    private String infoTypeId = "1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_clues);
        //showDialog();
        initJsonData();
        initData();
        initTitle();
        initView();
        initLister();
    }

    private void getCustomerType() {
        XUtil.Post(Url.API_RECORD, null, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Gson gson = new Gson();
                CustomerTypeBean bean = gson.fromJson(result, CustomerTypeBean.class);
                customerTypeList = bean.getInfo().getCustomer_type();
                operator = bean.getInfo().getOperator();
                infoType = new ArrayList<String>();
                infoId = new ArrayList<String>();
                for (int i = 0; i < customerTypeList.size(); i++) {
                    infoType.add(customerTypeList.get(i).getClassname());
                    infoId.add(customerTypeList.get(i).getCustomer_type_id());
                }
            }

            @Override
            public void onFinished() {
                Log.e("================", "+" + isUpdate);
                if (isUpdate == 1) {
                    upLoad();
                } else
                    closeDialog();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void getUserInfo() {
        if (MyApplication.getInstance().getUserBean() == null) {
            return;
        }
        getCustomerType();
    }

    private int type = 0;
    private int isUpdate = 0;
    private String recordid;
    private boolean flag = false;

    private void initData() {
        type = getIntent().getIntExtra("type", 0);
        isUpdate = getIntent().getIntExtra("isUpdate", 0);
        recordid = getIntent().getStringExtra("recordid");
        TextView type_customer_name = (TextView) findViewById(R.id.type_customer_name);
        TextView type_customer_address_click = (TextView) findViewById(R.id.type_customer_address_click);
        TextView type_name = (TextView) findViewById(R.id.type_name);
        TextView type_address_click = (TextView) findViewById(R.id.type_address_click);
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);

        switch (type) {
            case 0:
                type_customer_name.setText("线索客户名称");
                type_customer_address_click.setText("线索客户地址");
                type_name.setText("线索项目名称");
                type_address_click.setText("线索项目地址");
                title.setText("线索信息");
                break;
            case 1:
                type_customer_name.setText("精确客户名称");
                type_customer_address_click.setText("精确客户地址");
                type_name.setText("精确项目名称");
                type_address_click.setText("精确项目地址");
                title.setText("精确信息");
                break;
        }

    }

    private void initView() {
        //  option2 = new OptionsPickerView(this);
        switchButton = (SwitchView) findViewById(R.id.switchButton);
        switchButton.setOpened(false);
        switchButton.setOnStateChangedListener(this);

        customer_type = (TextView) findViewById(R.id.customer_type);
        address_click = (TextView) findViewById(R.id.address_click);
        look = (TextView) findViewById(R.id.look);
        add_person = (TextView) findViewById(R.id.add_person);
        customer_address_click = (TextView) findViewById(R.id.customer_address_click);

        name = (EditText) findViewById(R.id.name);
        desicAdd = (EditText) findViewById(R.id.desicAdd);
        customer_name = (EditText) findViewById(R.id.customer_name);
        customer_desicAdd = (EditText) findViewById(R.id.customer_desicAdd);
        nums = (TextView) findViewById(R.id.nums);
        all_money = (EditText) findViewById(R.id.all_money);

        rivla1 = (LinearLayout) findViewById(R.id.rivla1);
        rivla2 = (LinearLayout) findViewById(R.id.rivla2);
        rivla_img2 = (ImageView) findViewById(R.id.rivla_img2);
        rivla_img1 = (ImageView) findViewById(R.id.rivla_img1);
        recycler_person = (RecyclerView) findViewById(R.id.recycler_person);
        recycler_person.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        addPersonAdapter = new AddPersonAdapter(this, addPersonBean);
        recycler_person.setAdapter(addPersonAdapter);
        submit_all = (Button) findViewById(R.id.submit_all);
        TextView submit = (TextView) findViewById(R.id.submit);
        getUserInfo();
        if (isUpdate == 1) {
            findViewById(R.id.ll_project_nums).setVisibility(View.VISIBLE);
            submit_all.setText("修改备案");
            submit.setVisibility(View.VISIBLE);
            submit.setOnClickListener(this);
            submit.setText("转为精确");
            look.setText("查看产品信息");
        }

    }

    private void initLister() {
        customer_type.setOnClickListener(this);
        address_click.setOnClickListener(this);
        customer_address_click.setOnClickListener(this);
        add_person.setOnClickListener(this);
        rivla1.setOnClickListener(this);
        rivla2.setOnClickListener(this);
        submit_all.setOnClickListener(this);
        look.setOnClickListener(this);
    }


    private void initTitle() {
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        ll_rootView = (LinearLayout) findViewById(R.id.ll_rootView);
    }

    private boolean isCustomerAdd, isAdd;
    private String s1 = "", s2 = "", s3 = "", s4 = "", s5 = "", s6 = "";

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.customer_type:
                pvOptions = new OptionsPickerView.Builder(AddAccurateActivity.this, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        //返回的分别是三个级别的选中位置
                        customer_type.setText(infoType.get(options1));
                        infoTypeId = infoId.get(options1);
                    }
                }).setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
                        .setTitleColor(getResources().getColor(R.color.text))//标题文字颜色)//标题文字颜色
                        .setSubmitColor(getResources().getColor(R.color.main_color))//确定按钮文字颜色
                        .setCancelColor(getResources().getColor(R.color.main_color))//取消按钮文字颜色
                        .build();
                pvOptions.setPicker(infoType);
                pvOptions.show();
                break;
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.customer_address_click:
                isCustomerAdd = true;
//                mPopWindow = new CityPickerPopWindow(this);
//                mPopWindow.showPopupWindow(ll_rootView);
//                mPopWindow.setCityPickListener(this);

                ShowPickerView(1);
                break;
            case R.id.address_click:
                isAdd = true;
//                mPopWindow = new CityPickerPopWindow(this);
//                mPopWindow.showPopupWindow(ll_rootView);
//                mPopWindow.setCityPickListener(this);
                ShowPickerView(2);
                break;
            case R.id.submit:
                AlertDialog.Builder builder = new AlertDialog.Builder(AddAccurateActivity.this);
                final View view = View.inflate(AddAccurateActivity.this, R.layout.is_conversion, null);
                builder.setView(view);
                final AlertDialog show = builder.show();
                Window dialogWindow = show.getWindow();
                WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                dialogWindow.setGravity(Gravity.CENTER);
                // lp.x = 100; // 新位置X坐标
                dialogWindow.setAttributes(lp);
                TextView quxiao1 = (TextView) view.findViewById(R.id.quxaio1);
                TextView queding1 = (TextView) view.findViewById(R.id.queding1);
                quxiao1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        show.dismiss();
                        return;
                    }
                });
                queding1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        type = 1;
                        show.dismiss();
                        if (setToast()) return;
                        upLoadSubmit();
                    }
                });
                break;
            case R.id.submit_all:
                if (setToast()) return;
                upLoadSubmit();
                break;
            case R.id.rivla1:
                rival_li1 = !rival_li1;
                rivla_img1.setImageResource(rival_li1 ? R.mipmap.ic_choose_li : R.mipmap.ic_choose_nor);
                break;
            case R.id.rivla2:
                rival_li2 = !rival_li2;
                rivla_img2.setImageResource(rival_li2 ? R.mipmap.ic_choose_li : R.mipmap.ic_choose_nor);
                break;
            case R.id.look:
                Intent intent = new Intent(this, SelectProductActivity.class);
                intent.putExtra("data", data);
                startActivityForResult(intent, LOOKNUM);
                break;

            case R.id.add_person:
                Intent intent2 = new Intent(this, AddPersonActivity.class);
                startActivityForResult(intent2, ADDPERSON);
                break;
        }
    }

    private boolean setToast() {
        if (customer_name.getText().toString().equals("")) {
            MyToast("请填写客户名称");
            return true;
        }
        if (customer_address_click.getText().toString().equals("")) {
            MyToast("请填写客户地址");
            return true;
        }
        if (customer_desicAdd.getText().toString().equals("")) {
            MyToast("请填写客户详细地址");
            return true;
        }
        if (customer_type.getText().toString().equals("")) {
            MyToast("请选择客户类型");
            return true;
        }
        if (name.getText().toString().equals("")) {
            MyToast("请填写项目名称");
            return true;
        }
        if (address_click.getText().toString().equals("")) {
            MyToast("请选择项目地址");
            return true;
        }
        if (desicAdd.getText().toString().equals("")) {
            MyToast("请填写详细地址");
            return true;
        }
        if (all_money.getText().toString().equals("")) {
            MyToast("请填写总预算");
            return true;
        }
        if (data.size() == 0) {
            MyToast("请选择产品");
            return true;
        }
        if (addPersonBean.size() == 0) {
            MyToast("请添加联系人");
            return true;
        }
        return false;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == LOOKNUM) {
            if (resultCode == RESULT_OK) {
                this.data = (ArrayList<ProductBean>) data.getSerializableExtra("data");
                if (this.data.size() > 0) {
                    look.setText("查看产品信息");
                } else {
                    look.setText("");
                }
//                Log.e("---", this.data.toString());
                int total = 0;
                for (int i = 0; i < this.data.size(); i++) {
                    total += this.data.get(i).getNumber();
                }
                nums.setText(total + "");
            }
        } else {
            if (requestCode == ADDPERSON) {
                if (resultCode == RESULT_OK) {
                    AddPersonBean bean = new AddPersonBean();
                    bean.setName(data.getStringExtra("name"));
                    bean.setEmail(data.getStringExtra("email"));
                    bean.setJob(data.getStringExtra("job"));
                    bean.setPhone(data.getStringExtra("phone"));
                    bean.setWx_num(data.getStringExtra("wx_num"));
                    addPersonBean.add(bean);
                    recycler_person.setVisibility(View.VISIBLE);
                    addPersonAdapter.notifyDataSetChanged();
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    //提交
    private void upLoadSubmit() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        StringBuffer sb = new StringBuffer("[");
        for (int i = 0; i < data.size(); i++) {
            sb.append("{\"top_cate_id\":\"" + data.get(i).getInfoId() + "\",\"two_cate_id\":\"" + data.get(i).getDetailId() + "\" ,\"bathgoods_id\":\"" + data.get(i).getDetail_goodsId() + "\",\"nums\":\"" + data.get(i).getNumber() + "\"}");
            if (i != data.size() - 1) {
                sb.append(",");
            }
        }
        sb.append("]");
        String[] addStr = new String[3];
        addStr[0] = s1;
        addStr[1] = s2;
        addStr[2] = s3;
        StringBuffer sb2 = new StringBuffer("[");
        for (int i = 0; i < addPersonBean.size(); i++) {
            sb2.append("{\"name\":\"" + addPersonBean.get(i).getName() + "\" ,\"telphone\":\"" + addPersonBean.get(i).getPhone() + "\",\"email\":\"" + addPersonBean.get(i).getEmail() + "\",\"wx\":\"" + addPersonBean.get(i).getWx_num() + "\" ,\"job\":\"" + addPersonBean.get(i).getJob() + "\"}");
            if (i != addPersonBean.size() - 1) {
                sb2.append(",");
            }
        }
        sb2.append("]");
        StringBuffer json_str = new StringBuffer();
        json_str.append("{ \"project_name\":\"" + name.getText().toString() + "\"," +
                "\"project_address\":\"" + desicAdd.getText().toString() + "\"," +
                "\"project_city\":\"" + s5 + "\"," +
                "\"customer_city\":\"" + s2 + "\"," +
                "\"customer_address\":\"" + customer_desicAdd.getText().toString() + "\"," +
                "\"project_num\":\"" + nums.getText() + "\", " +
                "\"budget_money\":\"" + all_money.getText().toString() + "\"," +
                "\"customer_is_area\":\"" + (flag ? 1 : 2) + "\"," +
                "\"type\":\"" + type + "\",\"customer_province\":\"" + s1 + "\"," +
                "\"customer_type\":\"" + infoTypeId + "\"," +
                "\"compet_person\":\"" + ((rival_li1 ? "1" : "0") + "-" + (rival_li2 ? "1" : "0")) + "\"," +
                "\"contact_person\":" + sb2 + "," +
                "\"project_district\":\"" + s6 + "\"," +
                "\"customer_district\":\"" + s3 + "\"," +
                "\"project_type\":" + sb.toString() + "," +
                "\"project_province\":\"" + s4 + "\"," +
                "\"customer_name\":\"" + customer_name.getText().toString() + "\"");
        if (MyApplication.getInstance().getUserBean().getIsHuaWuYuan().equals("1"))
            json_str.append(",\"operator\":\"" + 1 + "\"");
        json_str.append("}");
        Log.e((rival_li1 ? "1" : "0") + "-" + (rival_li2 ? "1" : "0") + "----222" + rival_li2, json_str.toString());
        map.put("json_str", json_str.toString());
        map.put("recordid", recordid);
        map.put("equipment", "android");
        map.put("version", "" + SystemUtils.getVersionName(AddAccurateActivity.this));
        showDialog();
        if (isUpdate == 1) {
            updateLoad(map);
        } else if (isUpdate == 0) {
            addUpload(map);
        }
    }

    private void updateLoad(Map<String, Object> map) {
        XUtil.Post(Url.API_MODIFYRECORD, map, new MyCallBack<String>() {

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("---", result);
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    info = jsonObject.optString("info");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();

                if (info.equals("修改失败")) {
                    info = "未修改信息";
                }
                MyToast(info);
                if (info.equals("修改成功"))
                    finish();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                ex.printStackTrace();
            }
        });
    }

    private void addUpload(Map<String, Object> map) {
        XUtil.Post(Url.API_ADDRECORD, map, new MyCallBack<String>() {

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("---", result);
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    info = jsonObject.optString("info");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
                MyToast(info);
                if (info.equals("添加成功")) {
                    finish();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                ex.printStackTrace();
            }
        });
    }

    private String info = "";
    private AddPersonAdapter addPersonAdapter;

    @Override
    public void toggleToOn(View view) {
        flag = true;
        switchButton.setOpened(true);
    }

    @Override
    public void toggleToOff(View view) {
        flag = false;
        switchButton.setOpened(false);
    }

    class AddPersonAdapter extends BaseRVAdapter {
        private List<AddPersonBean> addPersonBean = new ArrayList<>();

        /**
         * 构造器
         *
         * @param context 上下文
         * @param list    数据集合
         */
        public AddPersonAdapter(Context context, @NonNull List list) {
            super(context, list);
            addPersonBean = list;
        }

        @Override
        public int getLayoutId(int viewType) {
            return R.layout.item_add_person;
        }

        @Override
        public void onBind(BaseViewHolder holder, final int position) {
            holder.setImageResource(R.id.img_head, R.mipmap.headportrait);
            holder.setTextView(R.id.text_name, addPersonBean.get(position).getName());
            holder.getView(R.id.delete).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    showDialogv7("确定删除该联系人吗？", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            addPersonBean.remove(position);
                            notifyDataSetChanged();
                        }
                    });
                    if (addPersonBean.size() <= 0) {
                        recycler_person.setVisibility(View.GONE);
                    }
                }
            });
            holder.getView(R.id.img_head).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(AddAccurateActivity.this);
                    final View view = View.inflate(AddAccurateActivity.this, R.layout.dialog_person, null);
                    final TextView name, phone, job, email, wx_num;
                    name = (TextView) view.findViewById(R.id.name);
                    phone = (TextView) view.findViewById(R.id.phone);
                    job = (TextView) view.findViewById(R.id.job);
                    email = (TextView) view.findViewById(R.id.email);
                    wx_num = (TextView) view.findViewById(R.id.wx_num);
                    name.setText(addPersonBean.get(position).getName());
                    phone.setText(addPersonBean.get(position).getPhone());
                    job.setText(addPersonBean.get(position).getJob());
                    email.setText(addPersonBean.get(position).getEmail());
                    wx_num.setText(addPersonBean.get(position).getWx_num());

                    builder.setView(view);
                    final AlertDialog show = builder.show();
                    Window dialogWindow = show.getWindow();
                    WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                    dialogWindow.setGravity(Gravity.CENTER);
                    // lp.x = 100; // 新位置X坐标
                    lp.width = getWindowManager().getDefaultDisplay().getWidth() / 3 * 2; // 宽度
                    dialogWindow.setAttributes(lp);
                    //
                    TextView cancel = (TextView) view.findViewById(R.id.cancel33);
                    TextView call = (TextView) view.findViewById(R.id.call);
                    cancel.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            show.dismiss();
                        }
                    });
                    call.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            AlertDialog.Builder builder = new AlertDialog.Builder(AddAccurateActivity.this);
                            builder.setMessage("即将跳往拨打电话界面");

                            builder.setTitle("联系人");

                            builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                    Intent intent2 = new Intent(Intent.ACTION_DIAL);
                                    Uri data = Uri.parse("tel:" + phone.getText());
                                    intent2.setData(data);
                                    startActivity(intent2);
                                }
                            });

                            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                            builder.create().show();

                        }
                    });

                }
            });
        }


    }

    private boolean isArea;

    @Override
    public void pickValue(String value) {
        isArea = true;
        if (isAdd)
            address_click.setText(value);
        if (isCustomerAdd) {
            customer_address_click.setText(value);
            String str[] = new String[3];
            str[0] = s1;
            str[1] = s2;
            str[2] = s3;
            isTrue:
            for (int i = 0; i < MyApplication.getInstance().getUserBean().getAreaList().size(); i++) {
                for (int j = 0; j < str.length; j++) {
                    if (MyApplication.getInstance().getUserBean().getAreaList().get(i).getRegion_name().toString().contains(str[j]) || str[j].contains(MyApplication.getInstance().getUserBean().getAreaList().get(i).getRegion_name())) {
                        isArea = false;
                        break isTrue;
                    }
                }
            }
        }
        isCustomerAdd = false;
        isAdd = false;
    }

    //////==============修改=====================
    private void setUpdateData() {
        Log.e("---------", "+" + 1);
        for (int i = 0; i < contact_person.size(); i++) {
            AddPersonBean b = new AddPersonBean();
            b.setWx_num(contact_person.get(i).getWx());
            b.setPhone(contact_person.get(i).getTelphone());
            b.setJob(contact_person.get(i).getJob());
            b.setEmail(contact_person.get(i).getEmail());
            b.setName(contact_person.get(i).getName());
            addPersonBean.add(b);
        }
        Log.e("---------", "+" + 2);
        if (addPersonBean.size() > 0) {
            recycler_person.setVisibility(View.VISIBLE);
        }
        Log.e("---------", "+" + 3);
        addPersonAdapter.notifyDataSetChanged();
        //
        Log.e("---------", "+" + 4);
        for (int i = 0; i < project_type.size(); i++) {
            ProductBean b = new ProductBean();
            b.setNumber(Integer.parseInt(project_type.get(i).getNums()));
            b.setInfoId(project_type.get(i).getTop_cate_id());
            b.setDetailId(project_type.get(i).getTwo_cate_id());
            b.setDetail_goodsId(project_type.get(i).getBathgoods_id());
            b.setInfoStr(project_type.get(i).getTop_cate_name());
            b.setDetailStr(project_type.get(i).getTwo_cate_name());
            b.setDetail_goodsStr(project_type.get(i).getBathgoods_name());
            data.add(b);
        }

        Log.e("---------", "+" + 5);

        //
        customer_type = (TextView) findViewById(R.id.customer_type);
        Log.e("---------", "+" + 6 + "--" + info2.getCustomer_type());
        for (int i = 0; i < infoType2.size(); i++) {
            Log.e("---///", infoType2.get(i) + "----");
        }
        Log.e("---------", "+" + 6 + "--++" + infoType2.get(Integer.parseInt(info2.getCustomer_type()) - 1));

        customer_type.setText("" + infoType2.get(Integer.parseInt(info2.getCustomer_type()) - 1));

        Log.e("---------", "+" + 7);
        customer_name.setText(info2.getCustomer_name());
        Log.e("---------", "+" + 8);
        s4 = info2.getProject_province();
        s5 = info2.getProject_city();
        s6 = info2.getProject_district();
        address_click.setText(info2.getProject_province() + info2.getProject_city() + info2.getProject_district());
        s1 = info2.getCustomer_province();
        s2 = info2.getCustomer_city();
        s3 = info2.getCustomer_district();
        Log.e("---------", "+" + 9);
        customer_address_click.setText(info2.getCustomer_province() + info2.getCustomer_city() + info2.getCustomer_district());

        Log.e("---------", "+" + 10);
        customer_desicAdd.setText(info2.getCustomer_address());

        Log.e("---------", "+" + 11);
        name.setText(info2.getProject_name());

        Log.e("---------", "+" + 12);
        desicAdd.setText("" + info2.getProject_address());
        Log.e("---------", "+" + 7);
        nums.setText("" + info2.getProject_num());
        all_money.setText("" + info2.getBudget_money());
        try {
            String str[] = info2.getCompet_person().split("-");
            rival_li1 = str[0].equals("1");
            rival_li2 = str[1].equals("1");
        } catch (Exception e) {
        }

        Log.e("---------", "+" + 8);
        rivla_img1.setImageResource(rival_li1 ? R.mipmap.ic_choose_li : R.mipmap.ic_choose_nor);
        rivla_img2.setImageResource(rival_li2 ? R.mipmap.ic_choose_li : R.mipmap.ic_choose_nor);
        TextView project_nums = (TextView) findViewById(R.id.project_nums);
        project_nums.setText(info2.getProject_no());

        if (info2.getCustomer_is_area().equals("1")) {
            isArea = true;
        } else {
            isArea = false;
        }

        switchButton.setOpened(isArea);
        Log.e("---------", rival_li1 + "-" + rival_li2 + "-" + isArea);
        look = (TextView) findViewById(R.id.look);

    }

    private RecordDetailBean.InfoEntity info2 = new RecordDetailBean.InfoEntity();
    private List<RecordDetailBean.InfoEntity.ProjectTypeEntity> project_type = new ArrayList<>();
    private List<RecordDetailBean.InfoEntity.ContactPersonEntity> contact_person = new ArrayList<>();
    private ArrayList<String> infoType2 = new ArrayList<>();

    private void upLoad() {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("recordid", recordid);
        XUtil.Post(Url.API_RECORDDETAIL, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("----", result);
                Gson gson = new Gson();
                RecordDetailBean bean = gson.fromJson(result, RecordDetailBean.class);
                if (bean.getResult().equals("1")) {
                    info2 = bean.getInfo();
                    project_type = info2.getProject_type();
                    contact_person = info2.getContact_person();
                    getCustomerType2();
                }
            }

            @Override
            public void onFinished() {

                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                ex.printStackTrace();
            }
        });
    }

    private void getCustomerType2() {
        XUtil.Post(Url.API_RECORD, null, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("----API_RECORD", result);
                Gson gson = new Gson();
                CustomerTypeBean bean = gson.fromJson(result, CustomerTypeBean.class);
                List<CustomerTypeBean.InfoEntity.CustomerTypeEntity> customerTypeList = bean.getInfo().getCustomer_type();
                infoType2 = new ArrayList<String>();
                for (int i = 0; i < customerTypeList.size(); i++) {
                    infoType2.add(customerTypeList.get(i).getClassname());
                }

            }

            @Override
            public void onFinished() {
                closeDialog();
                setUpdateData();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void ShowPickerView(final int type) {// 弹出选择器
        OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                String tx = options1Items.get(options1).getPickerViewText() +
                        options2Items.get(options1).get(options2) +
                        options3Items.get(options1).get(options2).get(options3);
                if (type == 1) {
                    s1 = options1Items.get(options1).getPickerViewText();
                    s2 = options2Items.get(options1).get(options2);
                    s3 = options3Items.get(options1).get(options2).get(options3);
                } else {
                    s4 = options1Items.get(options1).getPickerViewText();
                    s5 = options2Items.get(options1).get(options2);
                    s6 = options3Items.get(options1).get(options2).get(options3);

                }
                //    tv_city.setText(tx);
//                Toast.makeText(.this,tx,Toast.LENGTH_SHORT).show();
                isArea = true;
                if (isAdd)
                    address_click.setText(tx);
                if (isCustomerAdd) {
                    customer_address_click.setText(tx);
                    String str[] = new String[3];
                    str[0] = s1;
                    str[1] = s2;
                    str[2] = s3;


                    isTrue:
                    for (int i = 0; i < MyApplication.getInstance().getUserBean().getAreaList().size(); i++) {
                        for (int j = 0; j < str.length; j++) {
                            if (MyApplication.getInstance().getUserBean().getAreaList().get(i).getRegion_name().toString().contains(str[j]) || str[j].contains(MyApplication.getInstance().getUserBean().getAreaList().get(i).getRegion_name())) {
                                isArea = false;
                                break isTrue;
                            }
                        }
                    }
                }

                isCustomerAdd = false;
                isAdd = false;

            }

        })

                .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
                .setTitleColor(getResources().getColor(R.color.text))//标题文字颜色)//标题文字颜色
                .setSubmitColor(getResources().getColor(R.color.main_color))//确定按钮文字颜色
                .setCancelColor(getResources().getColor(R.color.main_color))//取消按钮文字颜色.build();
                .build();
        /*pvOptions.setPicker(options1Items);//一级选择器
        pvOptions.setPicker(options1Items, options2Items);//二级选择器*/
        if (options1Items.size() == 0) {
            initJsonData();
            return;
        }
        pvOptions.setPicker(options1Items, options2Items, options3Items);//三级选择器
        pvOptions.show();
        Log.e("asdasdads", "Adaaaaaaaaaaaaaaaaaaaaa");
    }

    private ArrayList<JsonBean> options1Items = new ArrayList<>();
    private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();
    private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();

    private void initJsonData() {//解析数据

        /**
         * 注意：assets 目录下的Json文件仅供参考，实际使用可自行替换文件
         * 关键逻辑在于循环体
         *
         * */
        String JsonData = new GetJsonDataUtil().getJson(this, "province.json");//获取assets目录下的json文件数据

        ArrayList<JsonBean> jsonBean = GetJsonDataUtil.parseData(JsonData);//用Gson 转成实体

        /**
         * 添加省份数据
         *
         * 注意：如果是添加的JavaBean实体，则实体类需要实现 IPickerViewData 接口，
         * PickerView会通过getPickerViewText方法获取字符串显示出来。
         */
        options1Items = jsonBean;

        for (int i = 0; i < jsonBean.size(); i++) {//遍历省份
            ArrayList<String> CityList = new ArrayList<>();//该省的城市列表（第二级）
            ArrayList<ArrayList<String>> Province_AreaList = new ArrayList<>();//该省的所有地区列表（第三极）

            for (int c = 0; c < jsonBean.get(i).getCityList().size(); c++) {//遍历该省份的所有城市
                String CityName = jsonBean.get(i).getCityList().get(c).getName();
                CityList.add(CityName);//添加城市

                ArrayList<String> City_AreaList = new ArrayList<>();//该城市的所有地区列表

                //如果无地区数据，建议添加空字符串，防止数据为null 导致三个选项长度不匹配造成崩溃
                if (jsonBean.get(i).getCityList().get(c).getArea() == null
                        || jsonBean.get(i).getCityList().get(c).getArea().size() == 0) {
                    City_AreaList.add("");
                } else {

                    for (int d = 0; d < jsonBean.get(i).getCityList().get(c).getArea().size(); d++) {//该城市对应地区所有数据
                        String AreaName = jsonBean.get(i).getCityList().get(c).getArea().get(d);

                        City_AreaList.add(AreaName);//添加该城市所有地区数据
                    }
                }
                Province_AreaList.add(City_AreaList);//添加该省所有地区数据
            }

            /**
             * 添加城市数据
             */
            options2Items.add(CityList);

            /**
             * 添加地区数据
             */
            options3Items.add(Province_AreaList);
        }
    }

}
